package com.piece.admin.controller;

import com.piece.admin.api.dto.SysUserDTO;
import com.piece.admin.model.TbSysUser;
import com.piece.admin.service.TbSysMenuService;
import com.piece.admin.support.TranslateEntity;
import com.piece.core.framework.constant.ApplicationConstants;
import com.piece.core.framework.support.convert.Convert;
import com.piece.core.framework.support.response.BreadCrumb;
import com.piece.core.framework.util.basic.I18nUtil;
import com.piece.core.framework.util.object.BeanUtil;
import com.piece.core.web.controller.FrameWorkController;
import com.piece.core.web.util.AuthenticationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import javax.annotation.Resource;
import java.util.*;

public class ControllerSupport extends FrameWorkController {

    @Autowired
    protected TranslateEntity translateEntity;

    @Resource
    protected TbSysMenuService tbSysMenuService;

    protected TbSysUser getCurrentUser() {
        SysUserDTO sysUser = (SysUserDTO) AuthenticationUtil.getInstance(getRequest()).getLoginUser().getDetail();
        TbSysUser user = new TbSysUser();
        BeanUtil.copyProperties(sysUser, user, true);
        return user;
    }

    protected void setBreadCrumb() {
        String flag = applicationProperties.getValue(ApplicationConstants.APPLICATION_BREADCRUMB_ENABLE);
        if (null != flag && Convert.toBool(flag, true) == true) {
            String url = getRequest().getServletPath();
            List<BreadCrumb> crumbs = tbSysMenuService.findCrumbsByUrl(url, getCurrentUser());
            // 添加首页
            BreadCrumb crumb = BreadCrumb.builder()
                            .name(I18nUtil.message("view.home"))
                            .url("/api/home")
                            .icon("fa fa-home")
                            .build();

            crumbs.add(0, crumb);
            getRequest().setAttribute("breadcrumb", true);
            getRequest().setAttribute("breadcrumbs", crumbs);
        }
    }

    protected void setBaseModel() {
        setParameter("id", getRequest().getParameter("id"));
        setParameter("pid", getRequest().getParameter("pid"));
        setParameter("single", getRequest().getParameter("single"));
        setParameter("multi", getRequest().getParameter("multi"));
        setParameter("selected", getRequest().getParameter("selected"));
        setParameter("remember", getRequest().getParameter("remember"));
    }

    protected void setLayerInfo() {
        setParameter("layer_index", getRequest().getParameter("layer_index"));
        setParameter("layer_callback", getRequest().getParameter("layer_callback"));
    }

    protected void setAuthInfo() {
        setParameter("access_token", getRequest().getParameter("access_token"));
    }

    protected void setEntityInfo(Object entity) {
        translateEntity.setEntityInfo(entity);
    }

    protected void setEntityInfo(Object entity, boolean cache) {
        translateEntity.setEntityInfo(entity, cache);
    }
}
